import asyncio
import time
import aiomysql
import ygg_aiomysql as aiomysql


async def example_delete_table():
    """示例10: 删除MySQL表的完整流程"""
    # 生成唯一数据库名和表名
    timestamp = int(time.time())
    test_db_name = f"example_db_{timestamp}"
    test_table_name = f"example_table_{timestamp}"

    # 创建连接池并添加数据库
    pool = await aiomysql.new_pool(password="zhangdapeng520")
    await aiomysql.add_database(pool, test_db_name)
    pool.close()
    await pool.wait_closed()

    # 使用新数据库创建表
    pool = await aiomysql.new_pool(db=test_db_name, password="zhangdapeng520")
    columns = {
        "id": "INT PRIMARY KEY AUTO_INCREMENT",
        "name": "VARCHAR(50) NOT NULL",
        "created_at": "TIMESTAMP DEFAULT CURRENT_TIMESTAMP",
    }
    await aiomysql.add_table(pool, test_table_name, columns)

    # 删除表并验证
    await aiomysql.delete_table(pool, test_table_name)
    async with pool.acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute(f"SHOW TABLES LIKE '{test_table_name}'")
            assert await cur.fetchone() is None

    # 清理资源
    pool.close()
    await pool.wait_closed()
    pool = await aiomysql.new_pool(password="zhangdapeng520")
    await aiomysql.delete_database(pool, test_db_name)
    pool.close()
    await pool.wait_closed()


# 运行示例
if __name__ == "__main__":
    asyncio.run(example_delete_table())
